home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / MESSAGE.C < prev    next >
Text File  |  1992-02-12  |  17KB  |  586 lines

  1. /* --------- message.c ---------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. static int px = -1, py = -1;
  6. static int pmx = -1, pmy = -1;
  7. static int mx, my;
  8. static int handshaking = 0;
  9. static BOOL CriticalError;
  10. BOOL AllocTesting = FALSE;
  11. jmp_buf AllocError;
  12.  
  13. /* ---------- event queue ---------- */
  14. static struct events    {
  15.     MESSAGE event;
  16.     int mx;
  17.     int my;
  18. } EventQueue[MAXMESSAGES];
  19.  
  20. /* ---------- message queue --------- */
  21. static struct msgs {
  22.     WINDOW wnd;
  23.     MESSAGE msg;
  24.     PARAM p1;
  25.     PARAM p2;
  26. } MsgQueue[MAXMESSAGES];
  27.  
  28. static int EventQueueOnCtr;
  29. static int EventQueueOffCtr;
  30. static int EventQueueCtr;
  31.  
  32. static int MsgQueueOnCtr;
  33. static int MsgQueueOffCtr;
  34. static int MsgQueueCtr;
  35.  
  36. static int lagdelay = FIRSTDELAY;
  37.  
  38. static void (interrupt far *oldtimer)(void);
  39. static void (interrupt far *oldkeyboard)(void);
  40.  
  41. static int keyportvalue;    /* for watching for key release */
  42.  
  43. WINDOW CaptureMouse;
  44. WINDOW CaptureKeyboard;
  45. static BOOL NoChildCaptureMouse;
  46. static BOOL NoChildCaptureKeyboard;
  47.  
  48. static int doubletimer = -1;
  49. static int delaytimer  = -1;
  50. static int clocktimer  = -1;
  51.  
  52. static WINDOW Cwnd;
  53.  
  54. static void interrupt far newkeyboard(void)
  55. {
  56.     keyportvalue = inp(KEYBOARDPORT);
  57.     oldkeyboard();
  58. }
  59.  
  60. /* ------- timer interrupt service routine ------- */
  61. static void interrupt far newtimer(void)
  62. {
  63.     if (timer_running(doubletimer))
  64.         countdown(doubletimer);
  65.     if (timer_running(delaytimer))
  66.         countdown(delaytimer);
  67.     if (timer_running(clocktimer))
  68.         countdown(clocktimer);
  69.     oldtimer();
  70. }
  71.  
  72. static char ermsg[] = "Error accessing drive x";
  73.  
  74. /* -------- test for critical errors --------- */
  75. int TestCriticalError(void)
  76. {
  77.     int rtn = 0;
  78.     if (CriticalError)    {
  79.         rtn = 1;
  80.         CriticalError = FALSE;
  81.         if (TestErrorMessage(ermsg) == FALSE)
  82.             rtn = 2;
  83.     }
  84.     return rtn;
  85. }
  86.  
  87. /* ------ critical error interrupt service routine ------ */
  88. static void interrupt far newcrit(IREGS ir)
  89. {
  90.     if (!(ir.ax & 0x8000))     {
  91.         ermsg[sizeof(ermsg) - 2] = (ir.ax & 0xff) + 'A';
  92.         CriticalError = TRUE;
  93.     }
  94.     ir.ax = 0;
  95. }
  96.  
  97. static void StopMsg(void)
  98. {
  99.     if (oldtimer != NULL)    {
  100.         setvect(TIMER, oldtimer);
  101.         oldtimer = NULL;
  102.     }
  103.     if (oldkeyboard != NULL)    {
  104.         setvect(KEYBOARDVECT, oldkeyboard);
  105.         oldkeyboard = NULL;
  106.     }
  107.     restorevideo();
  108.     ClearClipboard();
  109.     ClearDialogBoxes();
  110.     restorecursor();    
  111.     unhidecursor();
  112.     hide_mousecursor();
  113. }
  114.  
  115. /* ------------ initialize the message system --------- */
  116. BOOL init_messages(void)
  117. {
  118.     AllocTesting = TRUE;
  119.     if (setjmp(AllocError) != 0)    {
  120.         StopMsg();
  121.         return FALSE;
  122.     }
  123.     initvideo();
  124.     resetmouse();
  125.     set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
  126.     savecursor();
  127.     hidecursor();
  128.     px = py = -1;
  129.     pmx = pmy = -1;
  130.     mx = my = 0;
  131.     CaptureMouse = CaptureKeyboard = NULL;
  132.     NoChildCaptureMouse = FALSE;
  133.     NoChildCaptureKeyboard = FALSE;
  134.     MsgQueueOnCtr = MsgQueueOffCtr = MsgQueueCtr = 0;
  135.     EventQueueOnCtr = EventQueueOffCtr = EventQueueCtr = 0;
  136.     if (oldtimer == NULL)    {
  137.         oldtimer = getvect(TIMER);
  138.         setvect(TIMER, newtimer);
  139.     }
  140.     if (oldkeyboard == NULL)    {
  141.         oldkeyboard = getvect(KEYBOARDVECT);
  142.         setvect(KEYBOARDVECT, newkeyboard);
  143.     }
  144.     setvect(CRIT, newcrit);
  145.     PostMessage(NULL,START,0,0);
  146.     lagdelay = FIRSTDELAY;
  147.     return TRUE;
  148. }
  149.  
  150. /* ----- post an event and parameters to event queue ---- */
  151. static void PostEvent(MESSAGE event, int p1, int p2)
  152. {
  153.     if (EventQueueCtr != MAXMESSAGES)    {
  154.         EventQueue[EventQueueOnCtr].event = event;
  155.         EventQueue[EventQueueOnCtr].mx = p1;
  156.         EventQueue[EventQueueOnCtr].my = p2;
  157.         if (++EventQueueOnCtr == MAXMESSAGES)
  158.             EventQueueOnCtr = 0;
  159.         EventQueueCtr++;
  160.     }
  161. }
  162.  
  163. /* ------ collect mouse, clock, and keyboard events ----- */
  164. static void near collect_events(void)
  165. {
  166.     static int ShiftKeys = 0;
  167.     int sk;
  168.     struct tm *now;
  169.     static BOOL flipflop = FALSE;
  170.     static char timestr[9];
  171.     int hr;
  172.  
  173.     /* -------- test for a clock event (one/second) ------- */
  174.     if (timed_out(clocktimer))    {
  175.         /* ----- get the current time ----- */
  176.         time_t t = time(NULL);
  177.         now = localtime(&t);
  178.         hr = now->tm_hour > 12 ?
  179.              now->tm_hour - 12 :
  180.              now->tm_hour;
  181.         if (hr == 0)
  182.             hr = 12;
  183.         sprintf(timestr, "%2d:%02d", hr, now->tm_min);
  184.         strcpy(timestr+5, now->tm_hour > 11 ? "pm " : "am ");
  185.         /* ------- blink the : at one-second intervals ----- */
  186.         if (flipflop)
  187.             *(timestr+2) = ' ';
  188.         flipflop ^= TRUE;
  189.         /* -------- reset the timer -------- */
  190.         set_timer(clocktimer, 1);
  191.         /* -------- post the clock event -------- */
  192.         PostEvent(CLOCKTICK, FP_SEG(timestr), FP_OFF(timestr));
  193.     }
  194.  
  195.     /* --------- keyboard events ---------- */
  196.     if ((sk = getshift()) != ShiftKeys)    {
  197.         ShiftKeys = sk;
  198.         /* ---- the shift status changed ---- */
  199.         PostEvent(SHIFT_CHANGED, sk, 0);
  200.     }
  201.  
  202.     /* ---- build keyboard events for key combinations that
  203.         BIOS doesn't report --------- */
  204.     if (sk & ALTKEY)    {
  205.         if (keyportvalue == 14)    {
  206.             waitforkeyboard();
  207.             PostEvent(KEYBOARD, ALT_BS, sk);
  208.         }
  209.         if (keyportvalue == 83)    {
  210.             waitforkeyboard();
  211.             PostEvent(KEYBOARD, ALT_DEL, sk);
  212.         }
  213.     }
  214.     if (sk & CTRLKEY)    {
  215.         if (keyportvalue == 82)    {
  216.             while (!(inp(0x60) & 0x80))
  217.             waitforkeyboard();
  218.             PostEvent(KEYBOARD, CTRL_INS, sk);
  219.         }
  220.     }
  221.     /* ----------- test for keystroke ------- */
  222.     if (keyhit())    {
  223.         static int cvt[] = {SHIFT_INS,END,DN,PGDN,BS,'5',
  224.                         FWD,HOME,UP,PGUP};
  225.         int c = getkey();
  226.  
  227.         /* -------- convert numeric pad keys ------- */
  228.         if (sk & (LEFTSHIFT | RIGHTSHIFT))    {
  229.             if (c >= '0' && c <= '9')
  230.                 c = cvt[c-'0'];
  231.             else if (c == '.' || c == DEL)
  232.                 c = SHIFT_DEL;
  233.             else if (c == INS)
  234.                 c = SHIFT_INS;
  235.         }
  236.         if (c != '\r' && (c < ' ' || c > 127))
  237.             clearBIOSbuffer();
  238.         /* ------ post the keyboard event ------ */
  239.         PostEvent(KEYBOARD, c, sk);
  240.     }
  241.  
  242.     /* ------------ test for mouse events --------- */
  243.     if (button_releases())    {
  244.         /* ------- the button was released -------- */
  245.         doubletimer = DOUBLETICKS;
  246.         PostEvent(BUTTON_RELEASED, mx, my);
  247.         disable_timer(delaytimer);
  248.     }
  249.     get_mouseposition(&mx, &my);
  250.     if (mx != px || my != py)  {
  251.         px = mx;
  252.         py = my;
  253.         PostEvent(MOUSE_MOVED, mx, my);
  254.     }
  255.     if (rightbutton())
  256.         PostEvent(RIGHT_BUTTON, mx, my);
  257.     if (leftbutton())    {
  258.         if (mx == pmx && my == pmy)    {
  259.             /* ---- same position as last left button ---- */
  260.             if (timer_running(doubletimer))    {
  261.                 /* -- second click before double timeout -- */
  262.                 disable_timer(doubletimer);
  263.                 PostEvent(DOUBLE_CLICK, mx, my);
  264.             }
  265.             else if (!timer_running(delaytimer))    {
  266.                 /* ---- button held down a while ---- */
  267.                 delaytimer = lagdelay;
  268.                 lagdelay = DELAYTICKS;
  269.                 /* ---- post a typematic-like button ---- */
  270.                 PostEvent(LEFT_BUTTON, mx, my);
  271.             }
  272.         }
  273.         else    {
  274.             /* --------- new button press ------- */
  275.             disable_timer(doubletimer);
  276.             delaytimer = FIRSTDELAY;
  277.             lagdelay = DELAYTICKS;
  278.             PostEvent(LEFT_BUTTON, mx, my);
  279.             pmx = mx;
  280.             pmy = my;
  281.         }
  282.     }
  283.     else
  284.         lagdelay = FIRSTDELAY;
  285. }
  286.  
  287. /* ----- post a message and parameters to msg queue ---- */
  288. void PostMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  289. {
  290.     if (msg == ENDDIALOG)    {
  291.         msg++;
  292.         --msg;
  293.     }
  294.     if (MsgQueueCtr != MAXMESSAGES)    {
  295.         MsgQueue[MsgQueueOnCtr].wnd = wnd;
  296.         MsgQueue[MsgQueueOnCtr].msg = msg;
  297.         MsgQueue[MsgQueueOnCtr].p1 = p1;
  298.         MsgQueue[MsgQueueOnCtr].p2 = p2;
  299.         if (++MsgQueueOnCtr == MAXMESSAGES)
  300.             MsgQueueOnCtr = 0;
  301.         MsgQueueCtr++;
  302.     }
  303. }
  304.  
  305. /* --------- send a message to a window ----------- */
  306. int SendMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  307. {
  308.     int rtn = TRUE, x, y;
  309.  
  310. #ifdef INCLUDE_LOGGING
  311.     LogMessages(wnd, msg, p1, p2);
  312. #endif
  313.     if (wnd != NULL)
  314.         switch (msg)    {
  315.             case PAINT:
  316.             case BORDER:
  317.                 /* ------- don't send these messages unless the
  318.                     window is visible -------- */
  319.                 if (isVisible(wnd))
  320.                     rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  321.                 break;
  322.             case RIGHT_BUTTON:
  323.             case LEFT_BUTTON:
  324.             case DOUBLE_CLICK:
  325.             case BUTTON_RELEASED:
  326.                 /* --- don't send these messages unless the
  327.                     window is visible or has captured the mouse -- */
  328.                 if (isVisible(wnd) || wnd == CaptureMouse)
  329.                     rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  330.                 break;
  331.             case KEYBOARD:
  332.             case SHIFT_CHANGED:
  333.                 /* ------- don't send these messages unless the
  334.                     window is visible or has captured the keyboard -- */
  335.                 if (!(isVisible(wnd) || wnd == CaptureKeyboard))
  336.                     break;
  337.             default:
  338.                 rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
  339.                 break;
  340.         }
  341.     /* ----- window processor returned true or the message was sent
  342.         to no window at all (NULL) ----- */
  343.     if (rtn != FALSE)    {
  344.         /* --------- process messages that a window sends to the
  345.             system itself ---------- */
  346.         switch (msg)    {
  347.             case STOP:
  348.                 StopMsg();
  349.                 break;
  350.             /* ------- clock messages --------- */
  351.             case CAPTURE_CLOCK:
  352.                 Cwnd = wnd;
  353.                 set_timer(clocktimer, 0);
  354.                 break;
  355.             case RELEASE_CLOCK:
  356.                 Cwnd = NULL;
  357.                 disable_timer(clocktimer);
  358.                 break;
  359.             /* -------- keyboard messages ------- */
  360.             case KEYBOARD_CURSOR:
  361.                 if (wnd == NULL)
  362.                     cursor((int)p1, (int)p2);
  363.                 else if (wnd == inFocus)
  364.                     cursor(GetClientLeft(wnd)+(int)p1,
  365.                                 GetClientTop(wnd)+(int)p2);
  366.                 break;
  367.             case CAPTURE_KEYBOARD:
  368.                 if (p2)
  369.                     ((WINDOW)p2)->PrevKeyboard=CaptureKeyboard;
  370.                 else
  371.                     wnd->PrevKeyboard = CaptureKeyboard;
  372.                 CaptureKeyboard = wnd;
  373.                 NoChildCaptureKeyboard = (int)p1;
  374.                 break;
  375.             case RELEASE_KEYBOARD:
  376.                 if (CaptureKeyboard == wnd || (int)p1)
  377.                     CaptureKeyboard = wnd->PrevKeyboard;
  378.                 else    {
  379.                     WINDOW twnd = CaptureKeyboard;
  380.                     while (twnd != NULL)    {
  381.                         if (twnd->PrevKeyboard == wnd)    {
  382.                             twnd->PrevKeyboard = wnd->PrevKeyboard;
  383.                             break;
  384.                         }
  385.                         twnd = twnd->PrevKeyboard;
  386.                     }
  387.                 }
  388.                 wnd->PrevKeyboard = NULL;
  389.                 NoChildCaptureKeyboard = FALSE;
  390.                 break;
  391.             case CURRENT_KEYBOARD_CURSOR:
  392.                 curr_cursor(&x, &y);
  393.                 *(int*)p1 = x;
  394.                 *(int*)p2 = y;
  395.                 break;
  396.             case SAVE_CURSOR:
  397.                 savecursor();
  398.                 break;
  399.             case RESTORE_CURSOR:
  400.                 restorecursor();
  401.                 break;
  402.             case HIDE_CURSOR:
  403.                 normalcursor();
  404.                 hidecursor();
  405.                 break;
  406.             case SHOW_CURSOR:
  407.                 if (p1)
  408.                     set_cursor_type(0x0106);
  409.                 else
  410.                     set_cursor_type(0x0607);
  411.                 unhidecursor();
  412.                 break;
  413.             case WAITKEYBOARD:
  414.                 waitforkeyboard();
  415.                 break;
  416.             /* -------- mouse messages -------- */
  417.             case RESET_MOUSE:
  418.                 resetmouse();
  419.                 set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
  420.                 break;
  421.             case MOUSE_INSTALLED:
  422.                 rtn = mouse_installed();
  423.                 break;
  424.             case MOUSE_TRAVEL:    {
  425.                 RECT rc;
  426.                 if (!p1)    {
  427.                     rc.lf = rc.tp = 0;
  428.                     rc.rt = SCREENWIDTH-1;
  429.                     rc.bt = SCREENHEIGHT-1;
  430.                 }
  431.                 else 
  432.                     rc = *(RECT *)p1;
  433.                 set_mousetravel(rc.lf, rc.rt, rc.tp, rc.bt);
  434.                 break;
  435.             }
  436.             case SHOW_MOUSE:
  437.                 show_mousecursor();
  438.                 break;
  439.             case HIDE_MOUSE:
  440.                 hide_mousecursor();
  441.                 break;
  442.             case MOUSE_CURSOR:
  443.                 set_mouseposition((int)p1, (int)p2);
  444.                 break;
  445.             case CURRENT_MOUSE_CURSOR:
  446.                 get_mouseposition((int*)p1,(int*)p2);
  447.                 break;
  448.             case WAITMOUSE:
  449.                 waitformouse();
  450.                 break;
  451.             case TESTMOUSE:
  452.                 rtn = mousebuttons();
  453.                 break;
  454.             case CAPTURE_MOUSE:
  455.                 if (p2)
  456.                     ((WINDOW)p2)->PrevMouse = CaptureMouse;
  457.                 else
  458.                     wnd->PrevMouse = CaptureMouse;
  459.                 CaptureMouse = wnd;
  460.                 NoChildCaptureMouse = (int)p1;
  461.                 break;
  462.             case RELEASE_MOUSE:
  463.                 if (CaptureMouse == wnd || (int)p1)
  464.                     CaptureMouse = wnd->PrevMouse;
  465.                 else    {
  466.                     WINDOW twnd = CaptureMouse;
  467.                     while (twnd != NULL)    {
  468.                         if (twnd->PrevMouse == wnd)    {
  469.                             twnd->PrevMouse = wnd->PrevMouse;
  470.                             break;
  471.                         }
  472.                         twnd = twnd->PrevMouse;
  473.                     }
  474.                 }
  475.                 wnd->PrevMouse = NULL;
  476.                 NoChildCaptureMouse = FALSE;
  477.                 break;
  478.             default:
  479.                 break;
  480.         }
  481.     }
  482.     return rtn;
  483. }
  484.  
  485. static WINDOW MouseWindow(int x, int y)
  486. {
  487.     /* ------ get the window in which a
  488.                     mouse event occurred ------ */
  489.     WINDOW Mwnd = inWindow(x, y);
  490.  
  491.     /* ---- process mouse captures ----- */
  492.     if (CaptureMouse != NULL)
  493.         if (Mwnd == NULL ||
  494.                 NoChildCaptureMouse ||
  495.                     GetParent(Mwnd) != CaptureMouse)
  496.             Mwnd = CaptureMouse;
  497.     return Mwnd;
  498. }
  499.  
  500. void handshake(void)
  501. {
  502.     handshaking++;
  503.     dispatch_message();
  504.     --handshaking;
  505. }
  506.  
  507. /* ---- dispatch messages to the message proc function ---- */
  508. BOOL dispatch_message(void)
  509. {
  510.     WINDOW Mwnd, Kwnd;
  511.     /* -------- collect mouse and keyboard events ------- */
  512.     collect_events();
  513.     /* --------- dequeue and process events -------- */
  514.     while (EventQueueCtr > 0)  {
  515.         struct events ev;
  516.             
  517.         ev = EventQueue[EventQueueOffCtr];
  518.         if (++EventQueueOffCtr == MAXMESSAGES)
  519.             EventQueueOffCtr = 0;
  520.         --EventQueueCtr;
  521.  
  522.         /* ------ get the window in which a
  523.                         keyboard event occurred ------ */
  524.         Kwnd = inFocus;
  525.  
  526.         /* ---- process keyboard captures ----- */
  527.         if (CaptureKeyboard != NULL)
  528.             if (Kwnd == NULL ||
  529.                     NoChildCaptureKeyboard ||
  530.                         GetParent(Kwnd) != CaptureKeyboard)
  531.                 Kwnd = CaptureKeyboard;
  532.  
  533.         /* -------- send mouse and keyboard messages to the
  534.             window that should get them -------- */
  535.         switch (ev.event)    {
  536.             case SHIFT_CHANGED:
  537.             case KEYBOARD:
  538.                 if (!handshaking)
  539.                     SendMessage(Kwnd, ev.event, ev.mx, ev.my);
  540.                 break;
  541.             case LEFT_BUTTON:
  542.                 if (!handshaking)    {
  543.                     Mwnd = MouseWindow(ev.mx, ev.my);
  544.                     if (!CaptureMouse ||
  545.                             (!NoChildCaptureMouse &&
  546.                                 GetParent(Mwnd) == CaptureMouse))
  547.                         if (Mwnd != inFocus)
  548.                             SendMessage(Mwnd, SETFOCUS, TRUE, 0);
  549.                     SendMessage(Mwnd, LEFT_BUTTON, ev.mx, ev.my);
  550.                 }
  551.                 break;
  552.             case BUTTON_RELEASED:
  553.             case DOUBLE_CLICK:
  554.             case RIGHT_BUTTON:
  555.                 if (handshaking)
  556.                     break;
  557.             case MOUSE_MOVED:
  558.                 Mwnd = MouseWindow(ev.mx, ev.my);
  559.                 SendMessage(Mwnd, ev.event, ev.mx, ev.my);
  560.                 break;
  561.             case CLOCKTICK:
  562.                 SendMessage(Cwnd, ev.event,
  563.                     (PARAM) MK_FP(ev.mx, ev.my), 0);
  564.                 break;
  565.             default:
  566.                 break;
  567.         }
  568.     }
  569.     /* ------ dequeue and process messages ----- */
  570.     while (MsgQueueCtr > 0)  {
  571.         struct msgs mq;
  572.  
  573.         mq = MsgQueue[MsgQueueOffCtr];
  574.         if (++MsgQueueOffCtr == MAXMESSAGES)
  575.             MsgQueueOffCtr = 0;
  576.         --MsgQueueCtr;
  577.         SendMessage(mq.wnd, mq.msg, mq.p1, mq.p2);
  578.         if (mq.msg == ENDDIALOG)
  579.             return FALSE;
  580.         if (mq.msg == STOP)
  581.             return FALSE;
  582.     }
  583.     return TRUE;
  584. }
  585.  
  586.